<template>
	<view>
		<uni-card :is-shadow="false" is-full>
			<text class="uni-h6">新华三H3C VPN插件</text>
		</uni-card>
		<uni-section title="参数" type="line" padding>
			<uni-easyinput v-model="userName" placeholder="用户名"></uni-easyinput>
			<uni-easyinput v-model="password" placeholder="密码"></uni-easyinput>
			<uni-easyinput v-model="addr" placeholder="IP地址:端口号"></uni-easyinput>
			<uni-easyinput v-model="domain" placeholder="域"></uni-easyinput>

		</uni-section>
		<uni-section title="日志" type="line" padding>

			<text class="uni-h6">{{portalLogoutWarn}}</text>
		</uni-section>
		<uni-section title="浏量" type="line" padding>

			<text class="uni-h6">{{trafficTxt}}</text>
		</uni-section>
		<uni-section title="操作" type="line">
			<button @click="vpnGetDomain" type="primary" class="mt-1 mx-1">vpn连接</button>
			<button @click="vpnLogin" type="primary" class="mt-1 mx-1">vpn认证</button>
			<button @click="connectWifi" type="primary" class="mt-1 mx-1">链接WiFi</button>
			<button @click="vpnCheckOnline" type="primary" class="mt-1 mx-1">vpn检测在线</button>
			<button @click="vpnLogout" type="primary" class="mt-1 mx-1">vpn下线</button>
			<button @click="vpnTrafficInfo" type="primary" class="mt-1 mx-1">获取流量统计</button>
		</uni-section>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				TAG: 'VpnModule',
				userName: 'test',
				password: '22024019',
				addr: '119.145.27.102:8899',
				sdk: null,
				portalLogoutWarn: '这里是VPN日志',
				domain: '',
				dns: '8.8.8.8',
				trafficTxt: '',
			}
		},
		onLoad() {
			if (uni.getSystemInfoSync().platform === 'android') {
				// 安卓平台的代码
				this.sdk = uni.requireNativePlugin('Huo15-sslvpn')
				this.sdk.requestPermissions()

			} else if (uni.getSystemInfoSync().platform === 'ios') {
				// iOS平台的代码
				this.sdk = uni.requireNativePlugin('FDVPNPlugin-FDSSLVPNModule')
			}

			const globalEvent = uni.requireNativePlugin('globalEvent')
			this.log(this.sdk)


			globalEvent.addEventListener('ui_msg', (e) => {
				this.log(JSON.stringify(e))
				this.portalLogoutWarn = e.portalLogoutWarn;
			});


		},
		methods: {
			log(msg, t = '') {
				console.log(this.TAG + ":" + t, msg);
			},
			vpnGetDomain() {
				this.log('vpnGetDomain')
				if (uni.getSystemInfoSync().platform === 'android') {
					// 安卓平台的代码
					this.sdk.sslVpnGetDomain({
						userName: this.userName,
						password: this.password,
						addr: this.addr,
						domain: this.domain
					})
				} else if (uni.getSystemInfoSync().platform === 'ios') {
					// iOS平台的代码
					this.sdk.connectionVpn({
						userName: this.userName,
						password: this.password,
						addr: this.addr,
						domain: this.domain
					})
				}



			},
			vpnLogin() {
				this.log('vpnLogin')
				if (uni.getSystemInfoSync().platform === 'android') {
					// 安卓平台的代码
					this.sdk.sslVpnLogin();

				} else if (uni.getSystemInfoSync().platform === 'ios') {
					// iOS平台的代码
					this.sdk.loginVpn({
						userName: this.userName,
						password: this.password,
					})
				}

			},
			vpnCheckOnline() {
				this.log('vpnCheckOnline')
				if (uni.getSystemInfoSync().platform === 'android') {
					// 安卓平台的代码
					this.sdk.vpnCheckOnline();
				} else if (uni.getSystemInfoSync().platform === 'ios') {
					// iOS平台的代码
					this.sdk.getVpnStatusCallback();
				}

				
			},
			vpnLogout() {
				this.log('vpnLogout')
				if (uni.getSystemInfoSync().platform === 'android') {
					// 安卓平台的代码
					this.sdk.vpnLogout();
				} else if (uni.getSystemInfoSync().platform === 'ios') {
					// iOS平台的代码
					this.sdk.logoutVpnCallback();
				}

				
			},

			vpnTrafficInfo() {
				this.log('vpnTrafficInfo')
				this.sdk.vpnTrafficInfo(res => {
					this.log(res, 'trafficInfo')
					//{"traffic":"0B","wifiTxBytes":0,"mobleRxBytes":0,"mobleTxBytes":0,"wifiRxBytes":0,"wifiTotalData":0,"packname":"com.huo15.plugindemo","mobleTotalData":0,"uid":10181}
					this.trafficTxt = JSON.stringify(res);
				});
			},


		},
		onUnload() {

		},
	}
</script>

<style lang="scss" scoped>
	.mt-1 {
		margin-top: 10rpx;
	}

	.mx-1 {
		margin-left: 10rpx;
		margin-right: 10rpx;
	}
</style>